<?php

tsload(APPS_PATH . '/admin/Lib/Action/AdministratorAction.class.php');

/**
 * 移动端设置.
 *
 * @author Medz Seven <lovevipdsw@vip.qq.com>
 **/
class MobileAction extends AdministratorAction
{
    protected $api_brand = 'https://base.zhuritec.com:8766/api/yibum/p/brand/';
    protected $api_brand_list = 'https://base.zhuritec.com:8766/api/yibum/p/brand/page';
    protected $api_designer = 'https://base.zhuritec.com:8766/api/yibum/p/designer/';
    protected $api_designer_list = 'https://base.zhuritec.com:8766/api/yibum/p/designer/page';
    protected $api_series = 'https://base.zhuritec.com:8766/api/yibum/p/brandAtlas/getByBrandAtlasById?atlasId=';
    protected $api_series_all_list = 'https://base.zhuritec.com:8766/api/yibum/p/brandAtlas/getAllAtlas';
    protected $api_series_brand_list = 'https://base.zhuritec.com:8766/api/yibum/p/brandAtlas/getByBrandId';

    /**
     * 前置方法.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function _initialize()
    {
        $this->assign('isAdmin', 1);
        parent::_initialize();
    }

    /**
     * 3G办广场轮播列表.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function w3gSlideShow()
    {
        // # 设置页面字段
        $this->pageKeyList = array('name', 'image', 'url', 'sort', 'doaction');

        // # 添加tab
        array_push($this->pageTab, array(
            'title' => '轮播列表',
            'tabHash' => 'w3gSlideShow',
            'url' => U('admin/Mobile/w3gSlideShow'),
        ));
        array_push($this->pageTab, array(
            'title' => '添加轮播',
            'tabHash' => 'addW3gSlideShow',
            'url' => U('admin/Mobile/addW3gSlideShow'),
        ));

        // # 分页获取数据，20条
        $list = D('w3g_slide_show')->order('sort')->findPage(20);

        // # 加入操作按钮
        foreach ($list['data'] as $key => $value) {
            // # 参数
            $aid = $value['image'];
            $id = $value['id'];

            // # 添加图片
            $value = '<a href="%s" target="_blank"><img src="%s" width="300px" height="140px"></a>';
            $value = sprintf($value, getImageUrlByAttachId($aid), getImageUrlByAttachId($aid, 300, 140));
            $list['data'][$key]['image'] = $value;

            // # 添加操作按钮
            $value = '[<a href="%s">编辑</a>]&nbsp;-&nbsp;[<a href="%s">删除</a>]';
            $value = sprintf($value, U('admin/Mobile/addW3gSlideShow', array('id' => $id, 'tabHash' => 'addW3gSlideShow')), U('admin/Mobile/delW3gSlideShwo', array('id' => $id)));
            $list['data'][$key]['doaction'] = $value;
        }

        // # 设置无选择按钮
        $this->allSelected = false;

        // # 显示列表，并注销list变量
        $this->displayList($list);
        unset($list);
    }

    /**
     * [添加|编辑]3G版广场轮播.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function addW3gSlideShow()
    {
        // # 添加tab
        array_push($this->pageTab, array(
            'title' => '轮播列表',
            'tabHash' => 'w3gSlideShow',
            'url' => U('admin/Mobile/w3gSlideShow'),
        ));
        array_push($this->pageTab, array(
            'title' => (isset($_GET['id']) ? '编辑' : '添加') . '轮播',
            'tabHash' => 'addW3gSlideShow',
            'url' => U('admin/Mobile/addW3gSlideShow'),
        ));

        // # 设置页面参数
        $this->pageKeyList = array('name', 'image', 'link_type', 'topic_list', 'designer_list', 'brand_list', 'series_list', 'newproduct_list', 'url', 'sort');
        $this->opt['link_type'] = ['0' => '请选择一个类型', '1' => '话题', '2' => '设计师', '3' => '链接', '4' => '品牌', '5' => '品牌系列', '6' => '新品列表'];
        // # 页面参数
        $data = array();
        $topic_list = [0 => '请选择一个话题'];
        $designer_list = [0 => '请选择一个设计师'];
        $brand_list = [0 => '请选择一个品牌'];
        $series_list = [0 => '请选择一个系列'];
        $newproduct_list = [0 => '请选择一个新品'];
        //获取所有话题
        $topic_rs = model('FeedTopic')->where(['status' => 1])->order('is_top desc,topic_id desc')->select();
        if ($topic_rs) {
            foreach ($topic_rs as $key => $topic) {
                $topic_list[$topic['topic_id']] = $topic['topic_name'];
            }
        }
        $get_brand_list = $this->brand_list();
        if ($get_brand_list['code'] == 200) {
            foreach ($get_brand_list['data']["rows"] as $k => $v) {
                $brand_list[$v['id']] = $v['brandName'];
            }
        }
        $get_designer_list = $this->designer_list();
        if ($get_designer_list['code'] == 200) {
            foreach ($get_designer_list['data']["rows"] as $k => $v) {
                $designer_list[$v['id']] = $v['name'];
            }
        }
        $get_series_list = $this->series_list();
        if ($get_series_list['code'] == 200) {
            foreach ($get_series_list['data']["rows"] as $k => $v) {
                $series_list[$v['id']] = $v['atlasName'];
            }
        }
        $get_newproduct_list = $this->product();
        if ($get_newproduct_list['code'] == 200) {
            foreach ($get_newproduct_list['data'] as $k => $v) {
                $newproduct_list[$v['out_link']] = $v['title'];
            }
        }
        // # 设置按钮名称
        $this->submitAlias = '添加轮播';

        // # 如果是编辑
        if (isset($_GET['id']) and intval($_GET['id'])) {
            // # 获取数据
            $data = D('w3g_slide_show')->where('`id` = ' . intval($_GET['id']))->find();
            // # 设置按钮名称
            $this->submitAlias = '保存编辑';
        }
        $this->notEmpty = array('name', 'image');
        // # 设置提交地址
        $this->savePostUrl = U('admin/Mobile/doAddW3gSlideShow', array('id' => $_GET['id']));
        $this->opt['designer_list'] = $designer_list;
        $this->opt['topic_list'] = $topic_list;
        $this->opt['brand_list'] = $brand_list;
        $this->opt['series_list'] = $series_list;
        $this->opt['newproduct_list'] = $newproduct_list;
        // # 输出页面
        $this->displayIndexCarouselConfig($data);
    }

    public function showFeedListByTopic()
    {
        $topic_id = $_GET['topic_id'];
        $search_word = $_GET['search_word'];
        if ($topic_id && $topic_id > 0) {
            $rs = model('Feed')->getListByTopic(['topic_id' => $topic_id]);
            $feedlist['nowPage'] = $rs['nowPage'];
            $feedlist['data'] = $rs['data'];
            $feedlist['totalPages'] = $rs['totalPages'];
            $feedlist['count'] = $rs['count'];
            $feedlist['html'] = $rs['html'];
            //$feedlist['sql']    = $rs['sql'];
            $this->ajaxReturn($feedlist, 'success', 200);
        }
        if (trim($search_word)) {
            $rs = model('Feed')->getListByTopic(['search_word' => $search_word]);
            $feedlist['nowPage'] = $rs['nowPage'];
            $feedlist['data'] = $rs['data'];
            $feedlist['totalPages'] = $rs['totalPages'];
            $feedlist['count'] = $rs['count'];
            $feedlist['html'] = $rs['html'];
            //$feedlist['sql']    = $rs['sql'];
            $this->ajaxReturn($feedlist, 'success', 200);
        }
    }

    /**
     * [添加|编辑]轮播.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function doAddW3gSlideShow()
    {
        $_POST['topic_list'] > 0 && $post_id = $_POST['topic_list'];
        $_POST['designer_list'] > 0 && $post_id = $_POST['designer_list'];
        $_POST['brand_list'] > 0 && $post_id = $_POST['brand_list'];
        $_POST['series_list'] > 0 && $post_id = $_POST['series_list'];
        $_POST['newproduct_list'] > 0 && $post_id = $_POST['newproduct_list'];

        // # 获取参数
        list($id, $name, $image, $url, $sort, $link_type) = array($_GET['id'], $_POST['name'], $_POST['image'], $_POST['url'], $_POST['sort'] ? $_POST['sort'] : 100, $_POST['link_type']);

        // # 安全过滤
        list($id, $name, $image, $url, $sort, $link_type) = array(intval($id), t($name), intval($image), t($url), intval($sort), intval($link_type));

        // # 组装数据
        $data = array(
            'name' => $name,
            'image' => $image,
            'url' => $url,
            'sort' => $sort,
            'link_type' => $link_type,
        );

        if (!empty($post_id)) {
            $data['post_id'] = intval($post_id);
        }

        // # 判断更新
        if ($id and D('w3g_slide_show')->where('`id` = ' . $id)->field('id')->count()) {
            D('w3g_slide_show')->where('`id` = ' . $id)->save($data);
            $this->success('编辑成功！');
        }

        // # 添加，失败则输出错误
        D('w3g_slide_show')->add($data) or $this->error('添加失败');
        // # 设置成功跳转地址
        $this->assign('jumpUrl', U('admin/Mobile/w3gSlideShow'));

        // # 提示成功！
        $this->success('添加成功！');
    }

    /**
     * 删除轮播.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function delW3gSlideShwo()
    {
        $id = intval($_GET['id']);
        D('w3g_slide_show')->where('`id` = ' . $id)->delete();
        $this->success('删除成功！');
    }

    /**
     * 手机版logo设置.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function w3gLogo()
    {
        // # 添加菜单
        array_push($this->pageTab, array(
            'title' => '开关设置',
            'tabHash' => 'setting',
            'url' => U('admin/Mobile/setting'),
        ));
        array_push($this->pageTab, array(
            'title' => 'Logo设置',
            'tabHash' => 'w3gLogo',
            'url' => U('admin/Mobile/w3gLogo'),
        ));

        $this->pageKeyList = array('logo');

        $this->displayConfig();
    }

    /**
     * 手机版 关于我们.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function w3gAbout()
    {
        $this->pageKeyList = array('about');

        array_push($this->pageTab, array(
            'title' => '关于我们',
            'tabHash' => 'w3gAbout',
            'url' => U('admin/Mobile/w3gAbout'),
        ));

        $this->displayConfig();
    }

    /**
     * 3G版本开关设置.
     *
     * @author Medz Seven <lovevipdsw@vip.qq.com>
     **/
    public function setting()
    {
        // # 添加菜单
        array_push($this->pageTab, array(
            'title' => '开关设置',
            'tabHash' => 'setting',
            'url' => U('admin/Mobile/setting'),
        ));
        array_push($this->pageTab, array(
            'title' => 'Logo设置',
            'tabHash' => 'w3gLogo',
            'url' => U('admin/Mobile/w3gLogo'),
        ));

        $this->pageKeyList = array('switch');

        $this->opt = array_merge($this->opt, array(
            'switch' => array(
                '1' => '开启',
                '0' => '关闭',
            ),
        ));

        $this->displayConfig();
    }

    /**
     * 品牌列表
     */
    public function brand_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];

        $list = $this->http_request($this->api_brand_list);
        $d = json_decode($list, true);

        if ($d['status'] == 200 && isset($d['data'])) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $d['data'];
        }

        if ($d['status'] == 200 && !isset($d['data'])) {
            $data['msg'] = '未找到品牌信息';
        }

        return ($data);

    }


    /**
     * 设计师列表
     */
    public function designer_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $url = $this->api_designer_list;
        $brand_id = isset($_GET['brand_id']) ? t($_GET['brand_id']) : '';
        if (!empty($brand_id)) {
            $url .= '?brandId=' . $brand_id;
        }

        $list = $this->http_request($url);
        $d = json_decode($list, true);

        if ($d['status'] == 200 && isset($d['data'])) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $d['data'];
        }

        if ($d['status'] == 200 && !isset($d['data'])) {
            $data['msg'] = '未找到设计师信息';
        }
        return $data;
    }

    /**
     * 系列列表
     */
    public function series_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $url = $this->api_series_all_list;
        $brand_id = isset($_GET['brand_id']) ? strval($_GET['brand_id']) : '';
        if (!empty($brand_id)) {
            $url = $this->api_series_brand_list . '?brandId=' . $brand_id . '&uid=';
        }
        $list = $this->http_request($url);
        $d = json_decode($list, true);
        if (isset($d['data']) && $d['status'] == 200) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $d['data'];
        }

        if ($d['status'] == 200 && !isset($d['data'])) {
            $data['msg'] = '未找到系列信息';
        }
        return $data;
    }

    /**
     * 新品列表
     */
    public function product()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $products = D('product')->field('product_id,title,out_link,picture,created')->where(array('state' => 1))->order('created desc')->select();
        if (!empty($products)) {
            foreach ($products as $key => &$val) {
                $picture = D('attach')->where(array('attach_id' => $val['picture']))->find();
                if (isset($picture)) {
                    $val['picture'] = getImageUrl($picture['save_path'] . $picture['save_name']);
                }
            }
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $products;
        }
        return $data;

    }

    protected function http_request($url, $data = null, $headers = array())
    {
        $curl = curl_init();
        if (count($headers) >= 1) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
} // END class MobileAction entends AdmininistratorAction
